// 微信支付
export function wxPayment(data) {
	return new Promise((resolve, reject) => {
		// 区分是用订单编号换取支付id支付和直接用支付id支持
		if (data.pay_id_status) { //直接使用支付id支付
			uni.login({
				success: res => {
					console.log(123)
					// 用支付id+code换取支付所需参数
					this.request({
						url: '/api/pay.we_chat/we_mini',
						method: 'GET',
						data: {
							pay_log_id: data.pay_log_id,
							code: res.code
						},
						success: res => {
							// 调支付
							uni.requestPayment({
								provider: "wxpay",
								timeStamp: res.data.timeStamp,
								nonceStr: res.data.nonceStr,
								package: res.data.package,
								signType: res.data.signType,
								paySign: res.data.paySign,
								success(res) {
									data.success && data.success(res)
									resolve(res)
								},
								fail(e) {
									data.fail && data.fail({
										code:501,
										msg:'支付失败',
										pay_log_id:data.pay_log_id
									})
									reject(e)
								}
							})
						},
						complete: prop => {
							data.complete && data.complete(prop)
						}
					})
				},
				fail: err => {
					uni.showToast({
						title: 'code获取失败',
						icon: 'none'
					})
				}
			})
		} else {
			// 用订单编号获取支付id
			console.log(data)
			this.request({
				url: '/api/user.order/pay_info',
				method: 'GET',
				data: {
					order_no: data.order_no
				},
				success: result => {
					console.log(result)
					uni.login({
						success: res => {
							console.log(123)
							// 用支付id+code换取支付所需参数
							this.request({
								url: '/api/pay.we_chat/we_mini',
								method: 'GET',
								data: {
									pay_log_id: result.data.pay_log_id,
									code: res.code
								},
								success: res => {
									// 调支付
									uni.requestPayment({
										provider: "wxpay",
										timeStamp: res.data
											.timeStamp,
										nonceStr: res.data.nonceStr,
										package: res.data.package,
										signType: res.data.signType,
										paySign: res.data.paySign,
										success(res) {
											console.log(res, '支持成功')
											resolve(res)
											data.success && data
												.success(res)
										},
										fail(e) {
											data.fail && data.fail(e)
											reject(e)
										}
									})
								},
								complete: err => {
									data.complete && data.complete()
								}
							})
						},
						fail: err => {
							uni.showToast({
								title: 'code获取失败',
								icon: 'none'
							})
						}
					})

				}
			})
		}
	})
}
// 积分支付  参数 订单支付id   > pay_log_id 
export function PointsPayment(data) {
	
	this.request({
		url: '/api/pay.integral/index',
		method: 'GET',
		data: {
			pay_log_id: data.data.pay_log_id
		},
		success:data.success,
		fail:data.fail,
		complete: () => {}
	});
}
